package com.gitee.feizns.quickstart.easyexcel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.gitee.feizns.dynamic.io.Ios;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 字符串到 URL 转换器
 * @author feizns
 * @since 2025/6/6
 */
public class StringUrlImageConverter implements Converter<String> {


    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) throws IOException {
        try ( InputStream is = new URL(value).openStream() ) {
            return new WriteCellData<>(Ios.asBytes(is));
        }
    }

}
